/**
 * GLOBALS
 * -COLAMIX
 *  COLAMIX.DB
 * -emit(msg name, data) / shorthandler function for send
 * -eventuel CLIENTS aber damit hat man zugriff auf alle socket verbindungen
 */
var audioManager = require('./audioManager')();
var on = module.exports;

/**
 * send a stream of mp3 chunks
 * @param e {
 * 		url: 'file url relative to /mp3',
 * 		off: 'offset to read file', 
 * }
 * @param socket the user socket
 */
on.getMediaStream = function(e, socket){
	if(!e.hasOwnProperty('url') || !e.hasOwnProperty('off')){
		//sende error back ... aber dann mit richtigem error handling
		console.log('fail')
		return false;
	}
	audioManager.getChunkStream(e.url, e.off,  function(err, buffer){
		socket.send(buffer, {binary: true});
	},function(){
		emit('mediaStreamFinished', {err:false});
	});
};
/**
 * send a json array of mediaItems
 * @param e {
 * 		url: 'folder url',
 * 		depth: 'depth of subfolders', 
 * }
 * @param socket the user socket
 */
on.getMedia = function(e, socket){
	db = COLAMIX.DB.getProvider('media');
	db.getMedia(e.url, e.depth, function(err, result){
		emit('getMedia', result);
	});
};
/**
 * send a json array of folders
 * @param e {
 * 		url: 'folder url',
 * 		depth: 'depth of subfolders', 
 * }
 * @param socket the user socket
 */
on.getMediaStructur = function(e, socket){
	var db = COLAMIX.DB.getProvider('media');
	db.getMediaStructur(e.url, e.depth, function(err, result){
		emit('getMediaStructur', result);
	})
};







